<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->library(array("form_validation", "auth"));
		$this->load->helper('url');

		if (!$this->auth->is_Login()) {
			redirect("user/login");
			exit();
		}

		$this->load->library(array('session'));
		$this->load->model(array('user_model', 'article_model'));
	}

	function index($page = 1)
	{
		$total = $this->article_model->num_rows();
		$nb_record_page = 3;

		//--- Paging
		$this->load->library('pagination');
		$config['base_url'] = site_url("blog");
		$config['total_rows'] = $total;
		$config['per_page'] = $nb_record_page;
		$config['uri_segment'] = 2;
		$config['num_links'] = 2;
		$config['use_page_numbers'] = TRUE;

		$config['cur_tag_open'] = '<div  class="btn btn-default btn-info">';
		$config['cur_tag_close'] = '</div>';

		$config['num_tag_open'] = '';
		$config['num_tag_close'] = '';

		$config['last_tag_open'] = '';
		$config['last_tag_close'] = '';

		$config['next_tag_open'] = '';
		$config['next_tag_close'] = '';

		$config['first_tag_open'] = '';
		$config['first_tag_close'] = '';

		$config['prev_tag_open'] = '';
		$config['prev_tag_close'] = '';

		$config['next_link'] = FALSE;
		$config['prev_link'] = FALSE;

		$config['first_link'] = '&lsaquo;&nbsp;&nbsp;First';
		$config['last_link'] = 'Last&nbsp;&nbsp;&rsaquo;';

		$config['anchor_class'] = 'class="btn btn-default a_btn" style="text-decoration: none;"';

		$this->pagination->initialize($config);

		$data['pagination'] = $this->pagination->create_links();
		$data['user_data'] = $this->user_model->getInfo($this->session->userdata('userid'));
		$data['articles'] = $this->article_model->getAllUserArticle($this->session->userdata('userid'), $nb_record_page * ($page - 1), $nb_record_page);

		//var_dump($page);
		//var_dump($data);
		$this->load->view("logged_in/blog/index", $data);


	}

	function all($page = 1) {

		$total = $this->article_model->num_user_rows($this->session->userdata('userid'));
		$nb_record_page = 10;

		//--- Paging
		$this->load->library('pagination');
		$config['base_url'] = site_url("blog/all");
		$config['total_rows'] = $total;
		$config['per_page'] = $nb_record_page;
		$config['uri_segment'] = 3;
		$config['num_links'] = 2;
		$config['use_page_numbers'] = TRUE;

		$config['cur_tag_open'] = '<div  class="btn btn-default btn-info">';
		$config['cur_tag_close'] = '</div>';

		$config['num_tag_open'] = '';
		$config['num_tag_close'] = '';

		$config['last_tag_open'] = '';
		$config['last_tag_close'] = '';

		$config['next_tag_open'] = '';
		$config['next_tag_close'] = '';

		$config['first_tag_open'] = '';
		$config['first_tag_close'] = '';

		$config['prev_tag_open'] = '';
		$config['prev_tag_close'] = '';

		$config['next_link'] = FALSE;
		$config['prev_link'] = FALSE;

		$config['first_link'] = '&lsaquo;&nbsp;&nbsp;First';
		$config['last_link'] = 'Last&nbsp;&nbsp;&rsaquo;';

		$config['anchor_class'] = 'class="btn btn-default a_btn" style="text-decoration: none;"';

		$this->pagination->initialize($config);

		$data['pagination'] = $this->pagination->create_links();

		$data['user_data'] = $this->user_model->getInfo($this->session->userdata('userid'));
		$data['articles'] = $this->article_model->getAllUserArticle($this->session->userdata('userid'), $nb_record_page * ($page - 1), $nb_record_page);
		$this->load->view('logged_in/blog/list_articles',$data);
	}

	function article($id = 0) {
		$data['user_data'] = $this->user_model->getInfo($this->session->userdata('userid'));
		$data['article'] = $this->article_model->get_article($id);
		$this->load->view('logged_in/blog/article',$data);
	}
	function edit($id = 0) {
		$data['error'] = '';
		if(isset($_POST['btn_edit'])) {
			$success = TRUE;
			//content & title
			$data  = array(
				"title" => $this->input->post("title"),
                "content" => $this->input->post("content")
			);
			if(!$this->article_model->updateArticle($data,$id))
				$success = FALSE;
			else
				$data['error'] = "Error on update article!";

			//image
			$img = "";
			if($_FILES['image']['name'] != NULL){
				//xoa avatar cu
				$img = 'images/avatar/'.$this->user_model->getInfo($this->session->userdata('userid'))['image'];
				if(file_exists($img))
					unlink($img);
				//upload file moi
				$config['upload_path']   = './images/article_image/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['encrypt_name']  = true; // ma hoa ten file
				$config['remove_spaces'] = true; // xoa khoang trang
				$this->load->library('upload', $config);

				if(!$this->upload->do_upload("image"))
				{
					$data['error'] .= '<br/>'.$this->upload->display_errors();
					$success = FALSE;
				}
				else
				{
					$img = $this->upload->data();
					$add['image'] = $img['file_name'];

					if(!$this->article_model->updateArticle($add, $id)) {
						$data['error'] .= "<br/>Failed on change image.";
						$success = FALSE;
					}
				}

			}
			if($success == TRUE)
				redirect('blog/article/'.$id);
			else {
				$data['user_data'] = $this->user_model->getInfo($this->session->userdata('userid'));
				$data['article'] = $this->article_model->get_article($id);
				$this->load->view('logged_in/blog/edit',$data);

			}
		}
		$data['user_data'] = $this->user_model->getInfo($this->session->userdata('userid'));
		$data['article'] = $this->article_model->get_article($id);
		$this->load->view('logged_in/blog/edit',$data);
	}
}